﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Text;

using Continental.Code.BL;
using Continental.Code.Entity;

namespace Continental.UI.TuDiem
{
    public partial class List : System.Web.UI.Page
    {
        public Int32 nhomID = -1;
        public String titleOrNameOfNhom = "";
        public Int32 quanHuyenID = -1;
        public String titleOrNameOfQuanHuyen = "";
        public Int32 duongPhoID = -1;
        public String titleOrNameOfDuongPho = "";

        public Int32 pageIndex = 1;
        public Int32 totalRecord = 0;
        public Int32 pageSize = 30;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                nhomID = Int32.Parse(Request.QueryString["NhomID"].ToString());
            }
            catch
            {
                Response.Redirect("/tu-diem/trang-chinh.aspx");
            }

            NhomTuDiem theNhomTuDiem = TuDiemProvider.GetNhomTuDiemByID(nhomID);

            if (theNhomTuDiem == null)
                Response.Redirect("/tu-diem/trang-chinh.aspx");
            else
                titleOrNameOfNhom = ConvertToUnSignForUrl(theNhomTuDiem.TitleOrName);

            try
            {
                quanHuyenID = Int32.Parse(Request.QueryString["QuanHuyenID"].ToString());
                QuanHuyen theQuanHuyen = ExtensionProvider.GetQuanHuyenByID(quanHuyenID);

                if (theQuanHuyen != null)
                    titleOrNameOfQuanHuyen = ConvertToUnSignForUrl(theQuanHuyen.TitleOrName);
                else
                    titleOrNameOfQuanHuyen = "tat-ca";
            }
            catch
            {
                quanHuyenID = -1;
                titleOrNameOfQuanHuyen = "tat-ca";
            }

            try
            {
                duongPhoID = Int32.Parse(Request.QueryString["DuongPhoID"].ToString());
                DuongPho theDuongPho = ExtensionProvider.GetDuongPhoByID(duongPhoID);

                if (theDuongPho != null)
                    titleOrNameOfDuongPho = ConvertToUnSignForUrl(theDuongPho.TitleOrName);
                else
                    titleOrNameOfDuongPho = "tat-ca";

            }
            catch
            {
                duongPhoID = -1;
                titleOrNameOfDuongPho = "tat-ca";
            }

            try
            {
                pageIndex = Int32.Parse(Request.QueryString["PageIndex"].ToString());
            }
            catch
            {
                pageIndex = 1;
            }

            this.TopMenu1.nhomID = nhomID;
            pageSize = this.Paging1.PageSize;

            DataSet dsResult = TuDiemProvider.GetTuDiemListByFilterPaging(nhomID, quanHuyenID, duongPhoID, pageIndex, pageSize, out totalRecord);
            this.rpTuDiemList.DataSource = dsResult;
            this.rpTuDiemList.DataBind();
            this.Paging1.TotalRecord = totalRecord;

            this.Paging1.PageURL = "/tu-diem/" + titleOrNameOfNhom
                                    + "/" + nhomID.ToString()
                                    + "/" + titleOrNameOfQuanHuyen
                                    + "/" + quanHuyenID.ToString()
                                    + "/" + titleOrNameOfDuongPho
                                    + "/" + duongPhoID.ToString();
        }

        public String ConvertToUnSignForUrl(String text)
        {
            return Code.Utility.HelpMethod.ConvertToUnSignForUrl(text);
        }
    }
}
